/**
 * @file lv_appmenu_demo.c
 * Images and other resources come from the internet. If there is any infringement, please contact me to delete them
 */

/*********************
 *      INCLUDES
 *********************/
#include "lvgl.h"
#include "lv_appmenu.h"
#include "../../widgets/lv_obj_ex/lv_obj_ex.h" /*lv_obj_delete_all_child()*/

/*APP DEMO*/
#include "lvgl/examples/lv_examples.h"
#include "lvgl/demos/lv_demos.h"

extern void lv_appmenu_demo(void);

extern void lvTMenu_Demo(void);
extern void lvTMenu_Demo_Delete(void);

extern void lvIME_Demo(void);
extern void lvIME_Demo_Delete(void);

extern void lvScrnExcel_Demo(unsigned char ExcelId);
extern void lvScrnExcel_Demo_Delete(void);

extern void TScrnExcel_Demo(unsigned char ExcelId);
extern void TScrnExcel_Demo_Delete(void);

extern void lv_toolbar_demo(void);
extern void lv_ledstrip_demo1(void);
extern void lv_ledstrip_demo2(void);
extern void lv_obj_matrix_demo(uint32_t mode);
extern void lv_obj_matrix_2048(void);

extern void lv_battery_demo(void);
extern lv_obj_t * create_icon_state_wifi(lv_obj_t *parent);

/*********************
 *      DEFINES
 *********************/

 #define APP_IMG_PATH "C:f:/LVGL_workspace/lv_ex"

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/


 /**********************
 *  STATIC VARIABLES
 **********************/

const lv_appicon_info_t appicon_info[] = {
    {0, APP_IMG_PATH"/app_icon/music.png",       "matrix0",},
    {1,  APP_IMG_PATH"/app_icon/weather.png",    "matrix1"},
    {2,  APP_IMG_PATH"/app_icon/gallery.png",    "matrix2"},
    {3,  APP_IMG_PATH"/app_icon/calendar.png",   "matrix3"},
    {4,  APP_IMG_PATH"/app_icon/security.png",   "matrix4"},
    {5,  APP_IMG_PATH"/app_icon/notes.png",      "matrix5"},
    {6,  APP_IMG_PATH"/app_icon/settings.png",   "ledstrip1"},
    {7,  APP_IMG_PATH"/app_icon/theme.png",      "ledstrip2"},
    {8, APP_IMG_PATH"/app_icon/xiaoai.png",      "toolbar", },

    {9, APP_IMG_PATH"/app_icon/apps.png",       "widgets"},
    {10, APP_IMG_PATH"/app_icon/downloads.png",  "music"},
    {11, APP_IMG_PATH"/app_icon/email.png",      "*stress"},
    {12, APP_IMG_PATH"/app_icon/fm.png",         "lvIME"},
    {13,  APP_IMG_PATH"/app_icon/clock.png",       "lvExcel"},
    {14,  APP_IMG_PATH"/app_icon/calculator.png",  "tExcel"},
    {15, APP_IMG_PATH"/app_icon/remote.png",      "base explorer"},
    {16, APP_IMG_PATH"/app_icon/compass.png",     "control explorer"},
    {17,  APP_IMG_PATH"/app_icon/files.png",      "FileManager"},
    {18, APP_IMG_PATH"/app_icon/video.png",      "lvTMenu"},
    {19, APP_IMG_PATH"/app_icon/mihome.png",     "*lvBenchmark"},
    {20, APP_IMG_PATH"/app_icon/read.png",       "battery"},
    {21, APP_IMG_PATH"/app_icon/scanner.png",    "iconwifi"},
    {22, APP_IMG_PATH"/app_icon/recorder.png",   "Recorder"},
    {23, APP_IMG_PATH"/app_icon/game.png",       "Game"},
    {24, APP_IMG_PATH"/app_icon/more.png",       "More"},
    {25, APP_IMG_PATH"/app_icon/hardware.png",   "Hardware"},
    {26, APP_IMG_PATH"/app_icon/github.png",     "Github"},
    {27, APP_IMG_PATH"/app_icon/gitee.png",      "Gitee"},
    {28, APP_IMG_PATH"/app_icon/java.png",       "Java"},
    {29, APP_IMG_PATH"/app_icon/python.png",     "Python"},
    {30, APP_IMG_PATH"/app_icon/word.png",       "Word"},
    {31, APP_IMG_PATH"/app_icon/excel.png",      "Excel"},
    {32, APP_IMG_PATH"/app_icon/powerpoint.png", "PowerPoint"},
    {33, APP_IMG_PATH"/app_icon/photoshop.png",  "PhotoShop"},
    {34, APP_IMG_PATH"/app_icon/potplayer.png",  "PotPlayer"},
    {35, APP_IMG_PATH"/app_icon/gugeliulanqi.png","Google"},
    {36, APP_IMG_PATH"/app_icon/everything.png", "Everything"},
    {37, APP_IMG_PATH"/app_icon/matlab.png",     "Matlab"},
    {38, APP_IMG_PATH"/app_icon/vscode.png",     "VScode"},
    {39, APP_IMG_PATH"/app_icon/visualstudio.png","VisualStudio"},
    {40, APP_IMG_PATH"/app_icon/microsoft.png",  "MicroSoft"},
    {41, APP_IMG_PATH"/app_icon/win10.png",      "WIN10"},
    {42, APP_IMG_PATH"/app_icon/huawei.png",     "Huawei"},
    {43, APP_IMG_PATH"/app_icon/hongmeng.png",   "Hongmeng"},
    {44, APP_IMG_PATH"/app_icon/xiaomi.png",     "Xiaomi"},
    {45, APP_IMG_PATH"/app_icon/jialichuang.png","Jialichuang"},
    {46, APP_IMG_PATH"/app_icon/facebook.png",   "Facebook"},
    {47, APP_IMG_PATH"/app_icon/twitter.png",    "Twitter"},
    {48, APP_IMG_PATH"/app_icon/tumblr.png",     "Tumblr"},
    {49, APP_IMG_PATH"/app_icon/pinterest.png",  "Pinterest"},
    {50, APP_IMG_PATH"/app_icon/nvidia.png",     "Nvidia"},
    {51, APP_IMG_PATH"/app_icon/xunlei.png",     "Xunlei"},
    {52, APP_IMG_PATH"/app_icon/gaodeditu.png",  "Amap"},
    {53, APP_IMG_PATH"/app_icon/bilibili.png",   "Bilibili"},
    {54, APP_IMG_PATH"/app_icon/douyin.png",     "Tiktok"},
    {55, APP_IMG_PATH"/app_icon/kugou.png",      "Kugou"},
    {56, APP_IMG_PATH"/app_icon/taobao.png",     "Taobao"},
    {57, APP_IMG_PATH"/app_icon/zhifubao.png",   "Alipay"},
    {58, APP_IMG_PATH"/app_icon/pinduoduo.png",  "Pinduoduo"},
    {59, APP_IMG_PATH"/app_icon/jingdong.png",   "Jingdong"},
    {60, APP_IMG_PATH"/app_icon/qq.png",         "QQ"},
    {61, APP_IMG_PATH"/app_icon/qqjisuban.png",  "QQmini"},
    {62, APP_IMG_PATH"/app_icon/weixin.png",     "Wechat"},
    {63, APP_IMG_PATH"/app_icon/qqmusic.png",    "QQmusic"},
    {64, APP_IMG_PATH"/app_icon/kuwo.png",       "Kuwo"},
    {65, APP_IMG_PATH"/app_icon/12306.png",      "12306"},
    {66, APP_IMG_PATH"/app_icon/eleme.png",      "Elemo"},
    {67, APP_IMG_PATH"/app_icon/meituan.png",    "Meituan"},
    {68, APP_IMG_PATH"/app_icon/weibo.png",      "Weibo"},
    {69, APP_IMG_PATH"/app_icon/xianyu.png",     "Xianyu"},
    {70, APP_IMG_PATH"/app_icon/phone.png",      "Phone"},
    {71, APP_IMG_PATH"/app_icon/messages.png",   "Message"},
    {72, APP_IMG_PATH"/app_icon/browser.png",    "Browser"},
    {73, APP_IMG_PATH"/app_icon/camera.png",     "Camera"},  /*app_camera_cb*/
};

static lv_appmenu_t app_menu;
static int16_t last_app_id = -1;
static uint16_t hfocus = 0;
static uint16_t vfocus = 0;
static uint16_t screen_w = 0;
static uint16_t screen_h = 0;

static void enter_app_event_handler(struct _lv_appmenu_t *appmenu, uint32_t info_ary_id, uint32_t app_id)
{
    if(app_id > 21) return;
    /*Enter the app*/
    hfocus = lv_appmenu_get_hfocus(appmenu);
    vfocus = lv_appmenu_get_vfocus(appmenu);
    lv_appmenu_delete(appmenu);

    last_app_id = app_id;
    if(app_id <= 5) lv_obj_matrix_demo(app_id);
    else if(app_id == 6) lv_ledstrip_demo1();
    else if(app_id == 7) lv_ledstrip_demo2();
    else if(app_id == 8) lv_toolbar_demo();
    else if(app_id == 9) lv_demo_widgets();
    else if(app_id == 10) lv_demo_music();
    else if(app_id == 11) lv_demo_stress();
    else if(app_id == 12) lvIME_Demo();
    else if(app_id == 13){
        lvScrnExcel_Demo(0);
        lv_obj_t *note = lv_label_create(lv_layer_top());
        lv_label_set_text(note, "本模块未适配鼠标操作场景!");
        lv_obj_set_style_bg_color(note, lv_color_make(0xff,0x00,0x00), 0);
        lv_obj_set_style_bg_opa(note, LV_OPA_50, 0);
        lv_obj_align(note, LV_ALIGN_BOTTOM_MID, 0, -30);
    }
    else if(app_id == 14) TScrnExcel_Demo(0);
    else if(app_id == 15) lv_example_file_explorer_1();
    else if(app_id == 16) lv_example_file_explorer_2();
    else if(app_id == 17) lv_example_file_explorer_3();
    else if(app_id == 18) lvTMenu_Demo();
    else if(app_id == 19) lv_demo_benchmark();
    else if(app_id == 20) lv_battery_demo();
    else if(app_id == 21){
        lv_obj_t * obj = create_icon_state_wifi(lv_screen_active());
        lv_obj_align(obj, LV_ALIGN_TOP_RIGHT,-50, 0);
        lv_animimg_set_duration(obj, 2000);
        lv_animimg_set_repeat_count(obj, LV_ANIM_REPEAT_INFINITE);
        lv_animimg_start(obj);
    }
}

static lv_obj_t *close_button = NULL;
static void close_app_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code != LV_EVENT_CLICKED) return;
    if(last_app_id < 0){
        /*if(screen_w) lv_obj_set_width(lv_layer_sys(), screen_w);*/
        /*if(screen_h) lv_obj_set_width(lv_layer_sys(), screen_h);*/
       return;
    }

    /*Special deletion*/
    if(last_app_id == 12) lvIME_Demo_Delete();
    else if(last_app_id == 13) lvScrnExcel_Demo_Delete();
    else if(last_app_id == 14) TScrnExcel_Demo_Delete();
    else if(last_app_id == 18) lvTMenu_Demo_Delete();

    /*delete apps with lv_screen_active() as the parent object*/
    lv_obj_delete_all_child(lv_screen_active());
    lv_obj_delete_other_child(lv_layer_top(), close_button);

    last_app_id = -1;
    lv_appmenu_demo();
}

char bg_image_path[128] = {APP_IMG_PATH"/app_bg/launcher_bg.jpg"};

struct _lv_appmenu_info_t appmenu_info = {
  /*global info*/
  bg_image_path,
  74, /*>0:Vertical growth; <0:Horizontal growth*/
  /*app icon matrix_info*/
  LV_ALIGN_OUT_TOP_MID, /*.matrix_align*/
  0, /*.cfg*/
  {8, 8, 8, 8}, /*.matrix_margin*/
  {150, 150},     /*.matrix_cellsize*/
  /*appicon info*/
  {0x00 | 4, 15, 15, 0, NULL, NULL, NULL, 0xCCCCCC, 0x00FF00}, /*.info*/
  appicon_info,
  /*enter app callback function*/
  enter_app_event_handler,
};

const uint16_t ex_demo_lut[] = {15,   18,12,13,14,  6,7,8,  0,1,2,3,4,5, 20,21};
const uint16_t inner_demo_lut[] = {6,    9,10,11,15,16,17};

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
void lv_appmenu_demo(void)
{
    if(screen_w == 0){
        screen_w = lv_obj_get_width(lv_layer_top());
        screen_h = lv_obj_get_height(lv_layer_top());
    }
    lv_appmenu_create(&app_menu, lv_screen_active(), &appmenu_info, ex_demo_lut);//NULL

    /* Create a close button to return to appmenu*/
    if(close_button == NULL){
      close_button = lv_button_create(lv_layer_top());
      lv_obj_set_size(close_button, 24, 24);
      lv_obj_set_align(close_button, LV_ALIGN_TOP_RIGHT);
      lv_obj_t  *label = lv_label_create(close_button);
      lv_label_set_text(label, LV_SYMBOL_CLOSE);
      lv_obj_set_align(label, LV_ALIGN_CENTER);
      lv_obj_add_event_cb(close_button, close_app_event_handler, LV_EVENT_CLICKED, NULL);
    }
    lv_appmenu_set_focus(&app_menu, hfocus, vfocus);
    lv_group_focus_obj(app_menu.obj_matrix);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
